package com.voice.call.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.service.VersionUpdateService;
import com.voice.call.utils.IpUtil;
import com.voice.call.utils.JSONUtil;
import com.voice.call.utils.StringUtil;


@RestController
@RequestMapping("/api/{version}/version/")
public class VersionUpdateController {
	private static final Logger log = LoggerFactory.getLogger(VersionUpdateController.class);
	
	@Resource
	private VersionUpdateService updateService;
	
	
	@ResponseBody
	@RequestMapping(value="/update", method = RequestMethod.POST, produces=Const.JSON_UTF8)
	public Result versionUpdate(HttpServletRequest request,@RequestBody Map<String, Object> params) {
		
		String lang = ConstLang.getLang(params);
		if (null == params || params.get("pv") == null || params.get("v")==null||params.get("versionCode")==null
				|| StringUtil.isEmpty((String) params.get("pv"))
				|| StringUtil.isEmpty((String) params.get("v"))
				|| StringUtil.isEmpty((String) params.get("versionCode"))) {
			//log.error(ResultEnum.PARAM_ERROR.getMsg());
			return new Result(ResultEnum.PARAM_ERROR,lang);
		}
		String ip = IpUtil.getIpAddr(request);
		
		// 判断入参
		//log.debug("参数:{}", JSONUtil.objectToJson(params));
				
		// 组装入参 - 调用业务层
		Result rs = updateService.checkUpdate(params,ip);
		// 组装返回参数
		return rs;
	}

}
